#include "../mq_server/mq_queue.hpp"
#include <gtest/gtest.h>

using namespace mq_server;

MsgQueueManager::ptr mqp;

class ExchangeTest : public testing::Environment {
    public: 
        virtual void SetUp() override {
            mqp = std::make_shared<MsgQueueManager>("./data/meta.db");
        }
        virtual void TearDown() override {
            mqp->clear();
            std::cout << "最后的清理！！\n";
        }
};

TEST(queue_test, insert_test) {
    std::unordered_map<std::string, std::string> map = {{"k1", "v1"}};
    mqp->DeclareMsgQueue("queue1", true, false, false, map);
    mqp->DeclareMsgQueue("queue2", true, false, false, map);
    mqp->DeclareMsgQueue("queue3", true, false, false, map);
    mqp->DeclareMsgQueue("queue4", true, false, false, map);
    ASSERT_EQ(mqp->size(), 4); 
}
TEST(queue_test, select_test) {
    ASSERT_EQ(mqp->Exists("queue1"), true);
    ASSERT_EQ(mqp->Exists("queue2"), true);
    ASSERT_EQ(mqp->Exists("queue3"), true);
    ASSERT_EQ(mqp->Exists("queue4"), true);
    MsgQueue::ptr mqmp = mqp->SelectMsgQueue("queue2");
    ASSERT_NE(mqmp.get(), nullptr);
    ASSERT_EQ(mqmp->_name, "queue2");
    ASSERT_EQ(mqmp->_durable, true);
    ASSERT_EQ(mqmp->_auto_delete, false);
    ASSERT_EQ(mqmp->_exclusive, false);
    ASSERT_EQ(mqmp->getArgs(), std::string("k1=v1"));
}

TEST(queue_test, remove_test) {
    mqp->DeleteMsgQueue("queue2");
    MsgQueue::ptr mqmp = mqp->SelectMsgQueue("queue2");
    ASSERT_EQ(mqmp.get(), nullptr);
    ASSERT_EQ(mqp->Exists("queue2"), false);
}

int main(int argc,char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ExchangeTest);
    RUN_ALL_TESTS();
    return 0;
}